<?php

namespace Modules\Chinadmin\Http\Controllers\Admin;

use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cache;
use Modules\System\Services\Mds\MDSArray;

class Index extends Controller
{
    /**
     * http://l.com/chinadmin/admin/index/index
     */
    public function index(){

        $data['user'] = Auth::guard('admin')->user();

        if(empty($data['user'])){
            header("Location: /login");
            exit;
        }

        return view('chinadmin::admin/index',$data);

    }

    /**
     * 生成菜单
     *  http://l.com/chinadmin/admin/index/menuTree
     */
    public function menuTree(){

        $AuthRole = Cache::get('AuthRole')->toArray();

        $AuthRole = MDSArray::menuTreeArr($AuthRole);
        $AuthRole = $this->menuTreeJson($AuthRole);

        echo json_encode($AuthRole);

    }

    private function menuTreeJson($AuthRole){

        $newData['homeInfo']['title'] = "首页";
        $newData['homeInfo']['href'] = "/chinadmin/admin/Statistics/index";

        $newData['logoInfo']['title'] = "Chinadmin";
        $newData['logoInfo']['image'] = "/Modules/system/layuimini/images/logo.png";
        $newData['logoInfo']['href'] = "";

        $newData['menuInfo'] = [];

        $one['title'] = "管理";
        $one['icon'] = "fa fa-address-book";
        $one['href'] = "";
        $one['target'] = "_self";
        $one['child'] = [];

        foreach($AuthRole as $k => $v){

            $two = [];
            $two['title'] = $v['name'];
            $two['href'] = $v['route'];
            $two['icon'] = $v['icon'];
            $two['target'] = "_self";
            $two['child'] = [];

            foreach($v['child'] as $kc => $vc){

                $three = [];
                $three['title'] = $vc['name'];
                $three['href'] = $vc['route'];
                $three['icon'] = $vc['icon'];
                $three['target'] = "_self";
                $two['child'][] = $three;
            }

            $one['child'][] = $two;

        }

        $newData['menuInfo'][] = $one;

        return $newData;

    }


}
